import 'package:flutter/material.dart';
import '../../config/string_config.dart';
import '../../config/color_config.dart';
import '../../config/text_style_config.dart';
import '../../utils/screen_util.dart';
import '../../widgets/menu/app_drawer.dart';
import '../../widgets/title/title_widget.dart';

class RoomCreatePage extends StatefulWidget {
  const RoomCreatePage({Key? key}) : super(key: key);

  @override
  State<RoomCreatePage> createState() => _RoomCreatePageState();
}

class _RoomCreatePageState extends State<RoomCreatePage> {
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
  final TextEditingController _nameController = TextEditingController();
  final TextEditingController _numberController = TextEditingController();
  final TextEditingController _machineFromController =
      TextEditingController(text: '1');
  final TextEditingController _machineToController =
      TextEditingController(text: '255');
  final TextEditingController _routerController =
      TextEditingController(text: '1');

  @override
  void dispose() {
    _nameController.dispose();
    _numberController.dispose();
    _machineFromController.dispose();
    _machineToController.dispose();
    _routerController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      appBar:  TitleWidget(isHome:true,context: context, scaffoldKey: _scaffoldKey),
      backgroundColor: ColorConfig.white,
      body: SafeArea(
        child: SingleChildScrollView(
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 16),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                _buildTitle(),
                const SizedBox(height: 16),
                _buildProgressCard(),
                const SizedBox(height: 32),
                _buildInputField(
                    _nameController, 'Room Name', 'Enter room name'),
                const SizedBox(height: 24),
                _buildInputField(
                    _numberController, 'Room Number', 'Enter room number'),
                const SizedBox(height: 24),
                Text('Machine Number Range', style: TextStyleConfig.itemLabel),
                const SizedBox(height: 8),
                Row(
                  children: [
                    Expanded(
                        child: _buildInputField(_machineFromController, '', '1',
                            isSmall: true)),
                    Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 8.0),
                      child: Text('To', style: TextStyleConfig.itemLabel),
                    ),
                    Expanded(
                        child: _buildInputField(_machineToController, '', '255',
                            isSmall: true)),
                  ],
                ),
                const SizedBox(height: 24),
                _buildRouterRow(),
                const SizedBox(height: 48),
                _buildConfirmButton(),
              ],
            ),
          ),
        ),
      ),
    );
  }
  Widget _buildTitle() {
    return Text(
      'Create New Room',
      style: TextStyleConfig.heading20.copyWith(
        color: Color(0xFF002B66),
        fontWeight: FontWeight.w700,
        fontSize: 22,
      ),
      textAlign: TextAlign.left,
    );
  }

  Widget _buildProgressCard() {
    return Container(
      width: double.infinity,
      padding: const EdgeInsets.all(20),
      decoration: BoxDecoration(
        color: ColorConfig.white,
        borderRadius: BorderRadius.circular(14),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.16),
            blurRadius: 12,
            offset: const Offset(0, 4),
          ),
        ],
      ),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(40),
        child: LinearProgressIndicator(
          value: 0.33,
          minHeight: 12,
          backgroundColor: const Color(0xFFEEEEEE),
          valueColor: const AlwaysStoppedAnimation<Color>(Color(0xFF002B66)),
        ),
      ),
    );
  }

  Widget _buildInputField(
      TextEditingController controller, String label, String hint,
      {bool isSmall = false}) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        if (label.isNotEmpty)
          Padding(
            padding: const EdgeInsets.only(bottom: 8.0),
            child: Text(label, style: TextStyleConfig.itemLabel),
          ),
        SizedBox(
          height: 48,
          child: TextField(
            controller: controller,
            keyboardType: TextInputType.number,
            style: const TextStyle(fontSize: 16),
            decoration: InputDecoration(
              hintText: hint,
              hintStyle: const TextStyle(
                color: Color(0xFFBDBDBD),
                fontSize: 16,
              ),
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(8),
                borderSide: const BorderSide(color: Color(0xFFE0E0E0)),
              ),
              enabledBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(8),
                borderSide: const BorderSide(color: Color(0xFFE0E0E0)),
              ),
              focusedBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(8),
                borderSide: const BorderSide(color: Color(0xFF002B66)),
              ),
              contentPadding:
                  const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
            ),
          ),
        ),
      ],
    );
  }

  Widget _buildRouterRow() {
    return Row(
      children: [
        Expanded(
          child: Text('Number of Routers', style: TextStyleConfig.itemLabel),
        ),
        SizedBox(
          width: 100,
          height: 48,
          child: TextField(
            controller: _routerController,
            keyboardType: TextInputType.number,
            style: const TextStyle(fontSize: 16),
            decoration: InputDecoration(
              hintText: '1',
              hintStyle: const TextStyle(
                color: Color(0xFFBDBDBD),
                fontSize: 16,
              ),
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(8),
                borderSide: const BorderSide(color: Color(0xFFE0E0E0)),
              ),
              enabledBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(8),
                borderSide: const BorderSide(color: Color(0xFFE0E0E0)),
              ),
              focusedBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(8),
                borderSide: const BorderSide(color: Color(0xFF002B66)),
              ),
              contentPadding:
                  const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
            ),
          ),
        ),
      ],
    );
  }

  Widget _buildConfirmButton() {
    return SizedBox(
      width: double.infinity,
      height: 48,
      child: ElevatedButton(
        style: ElevatedButton.styleFrom(
          backgroundColor: const Color(0xFF002B66),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(40),
          ),
          padding: EdgeInsets.zero,
        ),
        onPressed: () {
          // TODO: 提交逻辑
        },
        child: Text(
          'Confirm',
          style: TextStyleConfig.buttonText.copyWith(
              fontSize: 20, fontWeight: FontWeight.bold, color: Colors.white),
        ),
      ),
    );
  }
}
// 需要在 StringConfig 中新增 createNewRoomTitle、roomName、roomNameHint、roomCode、roomCodeHint、roomDesc、roomDescHint、roomRemark、roomRemarkHint、confirm 字段，且不与已有重复。 